/*
** str_to_wordtab.c for libmy.a in /home/vanet_c//proj-kw/lib
** 
** Made by charles-edouard vanet
** Login   <vanet_c@epitech.net>
** 
** Started on  Mon Dec 10 15:08:37 2012 charles-edouard vanet
** Last update Tue Dec 11 12:38:38 2012 charles-edouard vanet
*/

#include	<stdlib.h>

int	check_next_charcut_tab(char *src, char *charcut, int i)
{
  int	j;

  j = 0;
  while (charcut[j])
    {
      if (src[i + 1] == charcut[j])
	return (1);
      j++;
    }
  return (0);
}

int	my_count_tabs(char *src, char *charcut)
{
  int	i;
  int	c;
  int	result;

  i = 0;
  result = 0;
  while(src[i])
    {
      c = 0;
      while (charcut[c])
	{
	  if (src[i] == charcut[c])
	    if (check_next_charcut_tab(src, charcut, i) == 0)
	      result++;
	  c++;
	}
      i++;
    }
  return (result + 1);
}

int	count_tab_chars(char *src, char *charcut, int *i)
{
  int	c;
  int	count;
  int	j;

  count = 0;
  j = *i;
  while (src[j])
    {
      c = -1;
      while (charcut[++c])
	{
	  if (src[j] == charcut[c])
	    if (check_next_charcut_tab(src, charcut, j) == 0)
	      return (count);
	}
      count++;
      j++;
    }
  return (count);
}

char	*fill_tab(char *result, char *src, char *charcut, int *i)
{
  int	size_tab;
  int	j;
  int	c;

  j = 0;
  size_tab = count_tab_chars(src, charcut, i);
  if ((result = malloc(size_tab + 1)) == NULL)
    return (NULL);
  while (j < size_tab)
    {
      result[j] = src[*i];
      j++;
      *i = *i + 1;
    }
  *i = *i + 1;
  result[j] = '\0';
  while (j > 0)
    {
      c = -1;
      j--;
      while (charcut[++c])
	if (result[j] == charcut[c])
	  result[j] = '\0';
    }
  return (result);
}

char	**my_str_to_wordtab(char **tab, char *src, char *charcut)
{
  int	nb_tabs;
  int	t;
  int	i;

  i = 0;
  t = -1;
  nb_tabs = my_count_tabs(src, charcut);
  if ((tab = malloc(sizeof(char*) * (nb_tabs + 1))) == NULL)
    return (NULL);
  while (++t < nb_tabs)
    if ((tab[t] = fill_tab(tab[t], src, charcut, &i)) == NULL)
      return (NULL);
  tab[t] = NULL;
  return (tab);
}
